package com.tjrac.zhaopin.utils;

import com.baomidou.mybatisplus.extension.api.R;
import lombok.Data;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Data
public class SendMail {
    private final String MAIL_NAME = "85901009@qq.com";
    private final String MAIL_AUTH = "bzssclwkquylbjac";
    private String destMail;
    private String captcha;

    public SendMail(String destMail, String captcha) {
        this.destMail = destMail;
        this.captcha = captcha;
    }

    public int send() {
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.host", "smtp.qq.com");
        properties.setProperty("mail.smtp.port", "465");
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.debug", "true");
        properties.setProperty("mail.transport", "smtp");
        properties.setProperty("mail.smtp.ssl.enable", "true");
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(MAIL_NAME, MAIL_AUTH);
            }
        });
        Message message = new MimeMessage(session);
        try {
            message.setFrom(new InternetAddress("85901009@qq.com"));
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(destMail));
            message.setSubject("仁爱招聘网——密码找回");
            message.setContent("您的验证码是：" + captcha + "，此验证码将作为您所找回密码的唯一凭据！", "text/html;charset=UTF-8");
            Transport transport = session.getTransport();
            transport.connect(MAIL_NAME, MAIL_AUTH);
            Transport.send(message);
            return 0;
        } catch (MessagingException e) {
            e.printStackTrace();
            return -1;
        }
    }
}
